package com.shp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shp.pojo.*;
import com.shp.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ZWL
 * @version : 1.0
 * @date : 2020/2/23 15:13
 */
@Controller
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 根据商品id查询商品的详细信息
     *
     * @param model
     * @param id    商品id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String goodsDetail(Model model, @PathVariable("id") Integer id) {
        goodsService.setGoodsDetail(model, id);
        return "/goods/goods_detail";
    }


    /**
     * 发布商品
     *
     * @return "/goods/publish_goods"
     */
    @RequestMapping(value = "/publishGoods")
    public String publishGoods() {
        return "/goods/publish_goods";
    }

    /**
     * 提交发布的商品信息
     *
     * @return ResultInfo
     */
    @ResponseBody
    @RequestMapping(value = "/publishGoodsSubmit", method = RequestMethod.POST)
    public ResultInfo publishGoodsSubmit(Good goods, @RequestParam("images") MultipartFile[] files, HttpServletRequest request) {
        // 保存提交的商品信息
        boolean flag = goodsService.saveGoodsMsg(goods, files, request);
        ResultInfo info = new ResultInfo();
        if (!flag) {
            info.setErrorMsg("图片不合法");
        } else {
            info.setFlag(true);
        }
        return info;
    }

    /**
     * 发布评论
     *
     * @return ResultInfo
     */
    @ResponseBody
    @RequestMapping(value = "/comments/pub", method = RequestMethod.POST)
    public ResultInfo addComments(@RequestBody Comments comments, HttpServletRequest request) {
        // 响应对象
        ResultInfo info = new ResultInfo();
        return goodsService.addComments(info, comments, request);
    }

    /**
     * 跳转到分类页面，显示精选商品
     *
     * @return  "goods/category"
     */
    @RequestMapping(value = "/category", method = RequestMethod.GET)
    public String categoryPage(Model model) {
        int pageSize = 16;
        // 定义封装记录的集合
        List<GoodsExtend> goodsList = null;
        // 获取第pageNum页，pageSize条内容
        PageHelper.startPage(1, pageSize);
        goodsList = goodsService.getSelectedGoodsList();
        // 参数：结果集合，连续要显示的页数
        PageInfo<GoodsExtend> pageInfo = new PageInfo<GoodsExtend>(goodsList, 5);
        // 将商品添加到页面
        model.addAttribute("info", pageInfo);
        return "goods/category";
    }

    /**
     * 跳转到分类页面，显示精选商品
     *
     * @param id      商品分类id
     * @param pageNum 当前页码
     * @return
     */
    @RequestMapping(value = "/category/{id}/{pageNum}", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo<GoodsExtend> listCategoryGoodsByPage(@PathVariable(value = "id", required = true) Integer id,
                                                         @PathVariable(value = "pageNum", required = true) Integer pageNum) {
        // 定义每页显示16条记录
        int pageSize = 16;
        // 定义封装记录的集合
        List<GoodsExtend> goodsList = null;
        // 如果id不符合
        if (id < 0 || id > 7) {
            return null;
        } else {
            // 查询精选商品
            if (id == 0) {
                // 获取第pageNum页，pageSize条内容
                PageHelper.startPage(pageNum, pageSize);
                goodsList = goodsService.getSelectedGoodsList();
                // 参数：结果集合，连续要显示的页数
                PageInfo<GoodsExtend> pageInfo = new PageInfo<GoodsExtend>(goodsList, 6);
                return pageInfo;
            } else {
                // 其他分类商品
                // 获取第pageNum页，pageSize条内容
                PageHelper.startPage(pageNum, pageSize);
                goodsList = goodsService.getCategoryGoodsList(id);

            /*
            用PageInfo对将查询的结果进行包装
            PageInfo属性：
                getList()：查询结果
                getPageNum()：当前页码
                getPrePage()：上一页
                getNextPage()：下一页
                getPageSize()：当前页的实际记录数
                getSize()：当前页固定记录数
                getPages()：总页码
                getTotal()：总记录数
             */
                // 参数：结果集合，连续要显示的页数
                PageInfo<GoodsExtend> pageInfo = new PageInfo<GoodsExtend>(goodsList, 6);
                // 返回前台
                return pageInfo;
            }
        }
    }

    /**
     * 模糊查询商品
     *
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String searchGoods(@RequestParam(value = "str", required = true) String str, Model model) {
        // 定义每页显示16条记录
        int pageSize = 16;
        // 模糊查询
        List<GoodsExtend> goodsList = null;
        if ("".equals(str)) {
            model.addAttribute("list", null);
        } else {
            // 获取第1页，pageSize条内容
            PageHelper.startPage(1, pageSize);
            goodsList = goodsService.searchGoodsList(str, str);
            PageInfo<GoodsExtend> pageInfo = new PageInfo<GoodsExtend>(goodsList, 6);
            model.addAttribute("info", pageInfo);
        }
        model.addAttribute("searchStr", str);
        return "goods/searchResultPage";
    }

    /**
     * 模糊查询商品 , 分页
     *
     * @param pageNum 当前页码
     * @return
     */
    @RequestMapping(value = "/search/{pageNum}", method = RequestMethod.GET)
    public String searchGoodsPage(@PathVariable(value = "pageNum", required = true) Integer pageNum,
                                    @RequestParam(value = "str", required = true) String str,Model model) {
        // 定义每页显示16条记录
        int pageSize = 16;
        // 定义封装记录的集合
        List<GoodsExtend> goodsList = null;
        // 获取第pageNum页，pageSize条内容
        PageHelper.startPage(pageNum, pageSize);
        goodsList = goodsService.searchGoodsList(str, str);
        // 参数：结果集合，连续要显示的页数
        PageInfo<GoodsExtend> pageInfo = new PageInfo<>(goodsList, 6);
        model.addAttribute("info",pageInfo);
        model.addAttribute("searchStr", str);
        // 返回前台
        return "goods/searchResultPage";
    }
}
